﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HYSpace.SpaceManager.Manager
{
	public class DataManager
	{
		public Zone CurrentZone
		{
			get
			{
				return this.m_currentZone;
			}
			set
			{
				this.m_currentZone = value;
				this.m_zoneManager.CurrentZone = value;
			}
		}

		public DataManager(ExternalCommandData commandData)
		{
			this.m_commandData = commandData;
			this.m_levels = new List<Level>();
			this.Initialize();
			this.m_currentLevel = this.m_levels[0];
		}

		private void Initialize()
		{
			Dictionary<int, List<Space>> dictionary = new Dictionary<int, List<Space>>();
			Dictionary<int, List<Zone>> dictionary2 = new Dictionary<int, List<Zone>>();
			Document document = this.m_commandData.Application.ActiveUIDocument.Document;
			FilteredElementIterator elementIterator = new FilteredElementCollector(document).OfClass(typeof(Level)).GetElementIterator();
			FilteredElementIterator elementIterator2 = new FilteredElementCollector(document).WherePasses(new SpaceFilter()).GetElementIterator();
			FilteredElementIterator elementIterator3 = new FilteredElementCollector(document).OfClass(typeof(Zone)).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				Level level = element as Level;
				if (level != null)
				{
					this.m_levels.Add(level);
					dictionary.Add(level.Id.IntegerValue, new List<Space>());
					dictionary2.Add(level.Id.IntegerValue, new List<Zone>());
				}
			}
			elementIterator2.Reset();
			while (elementIterator2.MoveNext())
			{
				Element element2 = elementIterator2.Current;
				Space space = element2 as Space;
				if (space != null)
				{
					dictionary[space.LevelId.IntegerValue].Add(space);
				}
			}
			elementIterator3.Reset();
			while (elementIterator3.MoveNext())
			{
				Element element3 = elementIterator3.Current;
				Zone zone = element3 as Zone;
				if (zone != null && document.GetElement(zone.LevelId) != null)
				{
					dictionary2[zone.LevelId.IntegerValue].Add(zone);
				}
			}
			this.m_spaceManager = new SpaceManager(this.m_commandData, dictionary);
			this.m_zoneManager = new ZoneManager(this.m_commandData, dictionary2);
		}

		public ReadOnlyCollection<Level> Levels
		{
			get
			{
				return new ReadOnlyCollection<Level>(this.m_levels);
			}
		}

		public Level GetLevel(ElementId id)
		{
			foreach (Level level in this.m_levels)
			{
				if (level != null && level.Id == id)
				{
					return level;
				}
			}
			return null;
		}

		public void CreateZone()
		{
			if (this.m_defaultPhase == null)
			{
				Parameter parameter = this.m_commandData.Application.ActiveUIDocument.Document.ActiveView.GetParameter(BuiltInParameter.VIEW_PHASE);
				if (parameter == null)
				{
					TaskDialog.Show("盈建科", "当前活动视图不能创建分区！");
					return;
				}
				ElementId elementId = parameter.AsElementId();
				this.m_defaultPhase = (this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId) as Phase);
			}
			try
			{
				this.m_zoneManager.CreateZone(this.m_currentLevel, this.m_defaultPhase);
			}
			catch (Exception ex)
			{
				TaskDialog.Show("Revit", ex.Message);
			}
		}

		public bool DeleteZone(Zone zone)
		{
			return this.m_zoneManager.DeleteZone(zone);
		}

		public Zone CreateZoneEx(string zoneName)
		{
			if (this.m_defaultPhase == null)
			{
				Parameter parameter = this.m_commandData.Application.ActiveUIDocument.Document.ActiveView.GetParameter(BuiltInParameter.VIEW_PHASE);
				if (parameter == null)
				{
					TaskDialog.Show("盈建科", "当前活动视图不能创建分区！");
					return null;
				}
				ElementId elementId = parameter.AsElementId();
				this.m_defaultPhase = (this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId) as Phase);
			}
			try
			{
				Zone zone = this.m_zoneManager.CreateZoneEx(this.m_currentLevel, this.m_defaultPhase);
				zone.Name = zoneName;
				this.m_currentZone = zone;
				return zone;
			}
			catch (Exception ex)
			{
				TaskDialog.Show("Revit", ex.Message);
			}
			return null;
		}

		public void CreateSpaces()
		{
			if (this.m_defaultPhase == null)
			{
				Parameter parameter = this.m_commandData.Application.ActiveUIDocument.Document.ActiveView.GetParameter(BuiltInParameter.VIEW_PHASE);
				if (parameter == null)
				{
					TaskDialog.Show("盈建科", "当前活动视图不能创建空间！");
					return;
				}
				ElementId elementId = parameter.AsElementId();
				this.m_defaultPhase = (this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId) as Phase);
			}
			try
			{
                if ((int)this.m_commandData.Application.ActiveUIDocument.Document.ActiveView.ViewType == 1)
				{
					this.m_spaceManager.CreateSpaces(this.m_currentLevel, this.m_defaultPhase);
				}
				else
				{
					TaskDialog.Show("Revit", "You can not create spaces in this plan view");
				}
			}
			catch (Exception ex)
			{
				TaskDialog.Show("Revit", ex.Message);
			}
		}

		public List<Level> GetLevels()
		{
			return this.m_levels;
		}

		public List<Space> GetSpaces()
		{
			return this.m_spaceManager.GetSpaces(this.m_currentLevel);
		}

		public SpaceSet GetSpacesInDefaultZone()
		{
			SpaceSet spaceSet = new SpaceSet();
			foreach (Level currentLevel in this.m_levels)
			{
				this.m_currentLevel = currentLevel;
				foreach (Space space in this.GetSpaces())
				{
					if (space.Zone == null)
					{
						spaceSet.Insert(space);
					}
					else if (space.Zone.Name.Equals("默认"))
					{
						spaceSet.Insert(space);
					}
				}
			}
			return spaceSet;
		}

		public bool ZoneNameExist(string strZoneName)
		{
			foreach (Level level in this.m_levels)
			{
				if (level != null)
				{
					List<Zone>.Enumerator enumerator2 = this.m_zoneManager.GetZones(level).GetEnumerator();
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Name.Equals(strZoneName))
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		public List<Zone> GetZones()
		{
			return this.m_zoneManager.GetZones(this.m_currentLevel);
		}

		public void Update(Level level)
		{
			this.m_currentLevel = level;
		}

		public Level FindLevel(int id)
		{
			foreach (Level level in this.m_levels)
			{
				if (level.Id.IntegerValue.Equals(id))
				{
					return level;
				}
			}
			return null;
		}

		public void AddSpaces(SpaceSet spaces)
		{
			this.m_zoneManager.AddSpaces(spaces);
		}

		public void RemoveSpaces(SpaceSet spaces)
		{
			this.m_zoneManager.RemoveSpaces(spaces);
		}

		public void UpdataZoneSpaces(SpaceSet st)
		{
			SpaceSet spaces = this.m_currentZone.Spaces;
			SpaceSet spaceSet = new SpaceSet();
			foreach (object obj in st)
			{
				Space space = obj as Space;
				if (!spaces.Contains(space))
				{
					spaceSet.Insert(space);
				}
			}
			this.m_currentZone.AddSpaces(spaceSet);
			spaceSet.Clear();
			foreach (object obj2 in spaces)
			{
				Space space2 = obj2 as Space;
				if (!st.Contains(space2))
				{
					spaceSet.Insert(space2);
				}
			}
			this.m_currentZone.RemoveSpaces(spaceSet);
		}

		public ExternalCommandData m_commandData;

		private List<Level> m_levels;

		private SpaceManager m_spaceManager;

		private ZoneManager m_zoneManager;

		private Level m_currentLevel;

		private Phase m_defaultPhase;

		private Zone m_currentZone;
	}
}
